package com.zhuzi.guicollect.controller.fx;

import com.jfoenix.controls.JFXComboBox;
import com.jfoenix.controls.JFXTextField;
import de.felixroske.jfxsupport.FXMLController;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Control;
import javafx.scene.control.Label;
import javafx.util.StringConverter;
import lombok.extern.slf4j.Slf4j;
import org.controlsfx.validation.ValidationResult;
import org.controlsfx.validation.ValidationSupport;
import org.controlsfx.validation.Validator;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * @Author: MI
 * @Date: 2021/11/10/22:04
 * @Description:
 */
@Slf4j
@FXMLController
public class FnvshenController implements Initializable {
    /**
     * 下拉框
     */
    @FXML
    public JFXComboBox<Label> comBoxType;
    /**
     * 保存地址
     */
    @FXML
    public JFXTextField textFieldDir;
    /**
     * 请求类型
     */
    @FXML
    public JFXTextField textFieldUrl;

    /**
     * 校验
     */
    private ValidationSupport validationSupport = new ValidationSupport();


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //下拉框
        comBoxType.getItems().add(new Label("g"));
        comBoxType.getItems().add(new Label("gallery"));
        comBoxType.getItems().add(new Label("girl"));
        comBoxType.setEditable(false);
        comBoxType.setPromptText("选择类型");
        comBoxType.setConverter(new StringConverter<Label>() {
            @Override
            public String toString(Label object) {
                return object == null ? "" : object.getText();
            }

            @Override
            public Label fromString(String string) {
                return new Label(string);
            }
        });

        validationSupport.registerValidator(comBoxType, (Control c, String newValue) ->
                ValidationResult.fromErrorIf(comBoxType, "类型必须有", newValue == null || newValue.isEmpty())
                        .addInfoIf(comBoxType, "girl类型", "girl".equals(newValue))
        );
        validationSupport.registerValidator(textFieldDir, Validator.createEmptyValidator("保存地址必须填写"));
        validationSupport.registerValidator(textFieldUrl, Validator.createEmptyValidator("请求路劲必须填写"));
    }

    public void downPic(ActionEvent actionEvent) {
    }
}
